package org.example.demo1.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import org.example.demo1.common.PageData;
import org.example.demo1.common.ResponseData;
import org.example.demo1.domain.GoodsClassify;
import org.example.demo1.mapper.GoodsClassifyMapper;
import org.example.demo1.service.IGoodsClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class GoodsClassifyServiceImpl implements IGoodsClassifyService {

    @Autowired
    private GoodsClassifyMapper goodsClassifyMapper;

    @Override
    public ResponseData<GoodsClassify> addClassify(GoodsClassify goodsClassify) {
        if(goodsClassify.getClassifyId() == null){
            return ResponseData.fail("分类编码不能为空");
        }
        if(StrUtil.isBlank(goodsClassify.getClassifyName())){
            return ResponseData.fail("分类名称不能为空");
        }

        if (goodsClassify.getClassifyId().shortValue() < 6) {
            return ResponseData.fail("分类编码长度必须大于等于6");
        }
        GoodsClassify getClassifyById = goodsClassifyMapper.getClassifyByName(goodsClassify.getClassifyName());
        if(ObjectUtil.isNotEmpty(getClassifyById)){
            return ResponseData.fail("用户已存在",goodsClassify);
        }
        goodsClassify.setCreateTime(new Date());
        int i = goodsClassifyMapper.insertGoodsClassify(goodsClassify);
        if (i <= 0){
            return ResponseData.fail("请求失败");
        }
        return ResponseData.success(goodsClassify);
    }

    @Override
    public ResponseData<GoodsClassify> getClassifyById(Integer classifyId) {
        return goodsClassifyMapper.selectByClassifyId(classifyId);
    }

    @Override
    public ResponseData<GoodsClassify> getClassifyByClassNameLike(String classifyName) {
        return goodsClassifyMapper.selectByClassNameLike("%" + classifyName + "%");
    }

    @Override
    public ResponseData<GoodsClassify> updateClassify(GoodsClassify goodsClassify) {
        goodsClassify.setUpdateTime(new Date());
        return goodsClassifyMapper.updateGoodsClassify(goodsClassify);
    }

    @Override
    public ResponseData<GoodsClassify> deleteClassifyById(Integer classifyId) {
        return goodsClassifyMapper.deleteById(classifyId);
    }

    @Override
    public ResponseData<PageData<GoodsClassify>> getClassifiesPage(int pageNum, int pageSize, Integer classifyId) {
        return goodsClassifyMapper.page(pageNum, pageSize, classifyId);
    }

}


